<template>
  <div class="container">
    <div class="search-box">
      <van-search
        :value='keyword'
        placeholder="点击输入搜索内容"
        use-action-slot
        shape="round"
        @change="onChangeVal"
        @search="onSearch"
        :autofocus="true"
        ref="searchBox">
        <div class="search-btn" size="small" slot="action" @tap="onSearch2">搜索</div>
      </van-search>
    </div>
    <div class="main">
      <div class="order-list">
        <div class="order-list-item" v-for="(item, index) in orderList" :key="index" @click="queryOrderInfo(item)">
          <div class="order-list__header">
            <div class="v-align-c">
              <!-- 保税图标表示在此 -->
              <div class="ordet-type-tag">{{item.deliveryTypeText}}</div>
              <div class="v-align-c">
                <img class="width-24 height-24 border-r" v-if="item.customerAvatar" :src="item.customerAvatar" alt="">
                <div class="font-14 font-w mar-l-6">{{item.customerNickname}}</div>
              </div>
            </div>
            <div>{{orderStatusDict[item.orderStatus]}}</div>
          </div>
          <div class="order-list--goodswrap">
            <div class="order-list__goods" v-for="(good, gooodIndex) in item.orderGoodsList" :key="gooodIndex">
              <img class="goods-img" v-if="good.goodsImageUrl" :src="good.goodsImageUrl" alt="">
              <div class="flex-item mar-l-8">
                <div class="flex flex-col flex-between min-h-c">
                  <div class="width-per-100">
                    <div class="_line-1 font-14 font-w">{{good.goodsTitle}}</div>
                    <div v-if="good.skuDetail" class="font-12 font-color-9">{{good.skuDetail}}</div>
                  </div>
                  <div class="width-per-100">
                    <div class="flex v-align-c flex-between mar-t-23">
                      <div class="font-11" style="color:#F8303D;">￥<span class="font-16 font-w">{{good.goodsPrice}}</span></div>
                      <div class="font-12 font-color-9 pad-t-2">x{{good.goodsNum}}</div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="order-list__total">
            <div>共{{item.goodsNum}}件</div>
            <div class="order-list__price">
              <div>合计：<span>￥</span><span class="font-18">{{item.orderPrice}}</span></div>
              <div class="mar-t-4">
                <span>(含</span>
                <span v-if="item.taxAmount && (item.taxAmount * 1 > 0)">税费¥{{ item.taxAmount }}，</span>
                <span>运费¥{{ item.freight }}</span>
                <span>)</span>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="pad-10 mar-t-10 align-c" v-if="noData">
        <img v-if="fileSpace" mode="widthFix" class="width-153 height-75 mar-t-30" :src="fileSpace + '/images/2-0-kong.png'" />
        <div class="pad-t-20 font-14 font-gray">空空如也</div>
      </div>
    </div>
  </div>
</template>
<script>
import { listDeliveryType, pageProjectOrderByMemberId } from '@/api/order'
export default {
  data() {
    return {
      keyword: '',
      pullLoad: {
        loading: false,
        finished: false
      },
      currentPage: 0,
      orderStatusDict: {
        '0': '待付款',
        '1': '待配送',
        '2': '待提货',
        '3': '已提货',
        '4': '已完成',
        '20': '待配送',
        '-1': '已取消',
        '-2': '已取消'
      },
      orderList: [], // 订单列表
      orderTypeDict: {}, // 订单配送类型键值对
      noData: false // 订单列表是否有数据
    }
  },
  onLoad(params) {
    // 初始化data
    Object.assign(this.$data, this.$options.data())
    if ('searchvalue' in params) {
      this.keyword = params.searchvalue
    }
  },
  mounted() {
    this.listDeliveryType()
    this.pageProjectOrderByMemberId()
  },
  onReachBottom() {
    // 触底刷新
    if (!this.pullLoad.finished) {
      this.pageProjectOrderByMemberId()
    }
  },
  methods: {
    onChangeVal(e) {
      this.keyword = e.mp.detail
    },
    onSearch(e) {
      // 搜索
      this.currentPage = 0
      this.pullLoad.finished = false
      this.orderList = []
      this.pageProjectOrderByMemberId()
    },
    onSearch2(e) {
      // 搜索
      this.currentPage = 0
      this.pullLoad.finished = false
      this.orderList = []
      this.pageProjectOrderByMemberId()
    },
    listDeliveryType() {
      // 根据店主类型获取订单配送方式
      listDeliveryType(this.getCustomerId).then(response => {
        if (response.isError) return
        this.deliverydict = []
        this.orderTypeDict = response.data
        let resData = response.data
        Object.keys(resData).forEach(key => {
          this.deliverydict.push({
            text: resData[key],
            type: key
          })
        })
        this.deliverydict.unshift({
          text: '全部',
          type: ''
        })
      }).catch(() => {
      })
    },
    pageProjectOrderByMemberId() {
      // 获取订单列表页分页数据
      let params = {
        keyword: this.keyword,
        memberId: this.getCustomerId,
        orderStatusList: '',
        deliveryType: '',
        currentPage: this.currentPage,
        size: 10
      }
      this.noData = false
      pageProjectOrderByMemberId(params).then(response => {
        if (response.isError) return
        if (response.data.total === 0) {
          this.noData = true
        }
        response.data.records.forEach(item => {
          switch (item.deliveryType) {
            case 1:
              item['deliveryTypeText'] = '快递配送'
              if (item.crossBorderOrderType === 2) {
                item['deliveryTypeText'] = '跨境保税'
              }
              break
            case 2:
              item['deliveryTypeText'] = '到家自提'
              break
            case 3:
              item['deliveryTypeText'] = '社区团购'
              break
            case 4:
              item['deliveryTypeText'] = '门店自提'
              break
            case 5:
              item['deliveryTypeText'] = '到家配送'
              break
          }
          this.orderList.push(item)
        })
        this.currentPage++
        if (this.orderList.length >= response.data.total || this.orderList.length === 0) {
          this.pullLoad.finished = true
        }
      }).catch(() => {
      })
    },
    queryOrderInfo(order) {
      switch (order.deliveryType) {
        case 1:
          // 快递配送
          if (order.orderType === 30) {
            this.$pt.navigateTo({ url: '/pages/order/distribution/order/detail/main?orderId=' + order.orderId })
          } else if (order.orderType === 10) {
            this.$pt.navigateTo({ url: '/pages/order/detail/main?orderId=' + order.orderId })
          }
          break
        case 2:
          // 门店自提
          this.$pt.navigateTo({ url: '/pages/order/detail/main?orderId=' + order.orderId })
          break
        case 3:
          // 社区团购
          this.$pt.navigateTo({ url: '/pages/order/delivery-detail/main?orderId=' + order.orderId })
          break
        case 4:
          // 到家自提
          this.$pt.navigateTo({ url: '/pages/order/delivery-detail/main?orderId=' + order.orderId })
          break
        case 5:
          // 到家配送
          this.$pt.navigateTo({ url: '/pages/order/delivery-detail/main?orderId=' + order.orderId })
          break
        default:
          break
      }
    }
  }
}
</script>
<style lang="less" scoped>
.min-h-c{
  min-height: 100px;
}
.search-box {
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: white;
  z-index: 9;
  // 对搜索框进行改造
  /deep/ .van-search--show-action {
    background: unset !important;
    padding-right: unset;
  }
  /deep/ .van-cell {
    background: #f4f4f4;
    border-radius: 25px;
  }
  /deep/ .van-search__action {
    background: white;
    font-size: 20px;
    color: #333333;
    font-weight: bold;
  }
}
.main{
  padding-top: 44px;
  .order-list{
    padding: 0 12px 12px;
    &-item{
      box-shadow: 0 0 9px rgba(182, 182, 182, 0.33);
      border-radius: 10px;
      background-color: #FFF;
      margin-top: 15px;
    }
    &__header{
      height: 40px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10px;
    }
    &--goodswrap{
      padding: 0 10px 15px;
      border-bottom: 1px solid #F5F5F5;
    }
    &__goods{
      display: flex;
      justify-content: space-between;
      align-items: flex-start;
      margin-top: 15px;
      &:first-child{
        margin-top: 10px;
      }
      .goods-img{
        width: 100px;
        height: 100px;
        border-radius: 10px;
      }
      ._line-1{
        height: 22px;
        line-height: 22px;
        overflow:hidden;
        text-overflow:ellipsis;
        display:-webkit-box;
        -webkit-line-clamp:1;
        -webkit-box-orient:vertical;
        word-break:break-word;
      }
      .line-2{
        font-size: 14px;
        font-weight: bold;
        line-height: 1.3;
      }
    }
    &__total{
      font-size: 12px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 10px;
      height: 55px;
    }
    &__price{
      text-align: right;
      line-height: 1;
    }
    .ordet-type-tag{
      position: relative;
      width: 37px;
      height: 37px;
      margin-top: -10px;
      background-color: red;
      color: #FFF;
      line-height: 1.3;
      font-size: 12px;
      font-weight: bold;
      letter-spacing: 2px;
      display: flex;
      align-items: center;
      text-align: center;
      padding-left: 2px;
      box-sizing: border-box;
      border-radius: 0 0 5px 5px;
      // writing-mode: vertical-rl;
      &::before{
        content: '';
        position: absolute;
        left: -3px;
        top: 0px;
        width: 0;
        height: 0;
        border-bottom: 4px solid #DB041D;
        border-left: 3px solid transparent;
      }
      &::after{
        content: '';
        position: absolute;
        right: -3px;
        top: 0px;
        width: 0;
        height: 0;
        border-bottom: 4px solid #DB041D;
        border-right: 3px solid transparent;
      }
    }
  }
  .border-r{
    border-radius: 50%;
    margin-left: 8px;
  }
}
</style>
